home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-09-09 | 15.5 KB | 574 lines | [TEXT/CWIE] |
- // Simple framework for Macintosh sample code
- //
- // David Hayward and Nick Thompson
- // Developer Technical Support
- // AppleLink: DEVSUPPORT
- //
- // Copyrite 1995, Apple Computer,Inc
- //
- // This file contains the framework's AppleEvent handlers
- //
- // 9/13/94 nick first cut
- // 12/13/94 david several modifications
-
-
- #include <Types.h>
- #include <Memory.h>
- #include <AppleEvents.h>
- #include <Errors.h>
-
- #include "aeUtils.h"
-
- #include "appGlobals.h"
- #include "appAEvts.h"
- #include "appPrint.h"
- #include "appErrors.h"
-
- #include "win.h"
- #include "winTables.h"
-
- /**\
- |**| ==============================================================================
- |**| PRIVATE FUNCTION PROTOTYPES
- |**| ==============================================================================
- \**/
- OSErr appHandleOAPP ( void ) ;
- OSErr appHandleODOC ( FSSpec *spec ) ;
- OSErr appHandlePDOC ( FSSpec *spec, FSSpec *dtp ) ;
- OSErr appHandleQUIT ( void ) ;
-
-
- /**\
- |**| ==============================================================================
- |**| PUBLIC FUNCTIONS
- |**| ==============================================================================
- \**/
-
-
- //-----------------------------------------------------------------------
- // called to register our appleevent handlers
-
- OSErr RegisterAppleEvents ( void )
- {
- OSErr err ;
-
- err = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication,
- NewAEEventHandlerProc(HandleOAPP),
- 0L, false ) ;
- if ( err ) return err ;
-
- err = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments,
- NewAEEventHandlerProc(HandleODOC),
- 0L, false ) ;
- if ( err ) return err ;
-
- #if PIGS_SHELL_PRINT
- err = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments,
- NewAEEventHandlerProc(HandlePDOC),
- 0L, false ) ;
- if ( err ) return err ;
- #endif
-
- err = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication,
- NewAEEventHandlerProc(HandleQUIT),
- 0L, false ) ;
- return err ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- HandleOAPP
- *------------------------------------------------------------------------------*
- This routine processes the 'OAPP' AppleEvent
- and passes it to the framework.
- \*------------------------------------------------------------------------------*/
- pascal OSErr HandleOAPP ( AppleEvent *theAppleEvent, AppleEvent *reply, long refCon )
- {
- OSErr err ;
-
- err = MyGotRequiredParams(theAppleEvent) ;
-
- if (err == noErr)
- err = appHandleOAPP() ;
-
- return err ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- HandleODOC
- *------------------------------------------------------------------------------*
- This routine processes the 'ODOC' AppleEvent
- and passes it to the framework.
- \*------------------------------------------------------------------------------*/
- pascal OSErr HandleODOC ( AppleEvent *theAppleEvent, AppleEvent *reply, long refCon )
- {
- FSSpec myFSS ;
- AEDescList docList ;
- OSErr err, ignoreErr ;
- long index, itemsInList ;
- Size actualSize ;
- AEKeyword keywd ;
- DescType returnedType ;
-
- err = AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &docList ) ;
- if (err == noErr)
- {
- // check for missing required parameters
- err = MyGotRequiredParams(theAppleEvent) ;
- if (err == noErr)
- {
- // see how many descriptor items are in the list
- // this is the number of documents we want to open
- err = AECountItems(&docList,&itemsInList) ;
-
- // now get each descriptor record from the list
- // coerce the returned data to an FSSpec record, and
- // open the asoociated file
-
- for (index=1; index <= itemsInList && err == noErr; index++)
- {
- err = AEGetNthPtr( &docList,
- index,
- typeFSS,
- &keywd,
- &returnedType,
- (Ptr)&myFSS,
- sizeof(myFSS),
- &actualSize) ;
-
- if (err == noErr)
- err = appHandleODOC( &myFSS ) ;
- }
- }
- ignoreErr = AEDisposeDesc(&docList) ;
- }
- return err ;
- }
-
-
- #if PIGS_SHELL_PRINT
- /*------------------------------------------------------------------------------*\
- HandlePDOC
- *------------------------------------------------------------------------------*
- This routine processes the 'PDOC' AppleEvent
- and passes it to the framework.
- \*------------------------------------------------------------------------------*/
- pascal OSErr HandlePDOC ( AppleEvent *theAppleEvent, AppleEvent *reply, long refCon )
- {
- FSSpec myFSS, dtpFSS ;
- AEDescList docList, dtpList ;
- OSErr err ;
- long index, itemsInList ;
- Size actualSize ;
- AEKeyword keywd ;
- DescType returnedType ;
- Boolean draggedToDTP = false ;
-
- err = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&docList) ;
- if (err == noErr)
- {
- // Check to see if the user dragged the document to a desktop printer.
- err = AEGetParamDesc(theAppleEvent, keyOptionalKeywordAttr,typeAEList,&dtpList) ;
- if (err == noErr) draggedToDTP = true ;
-
- // check for missing required parameters
- err = MyGotRequiredParams(theAppleEvent) ;
- if (err == noErr)
- {
- // get the desktop printer that the file(s) were draged to
- if (draggedToDTP)
- {
- err = AEGetNthPtr(&dtpList,
- 1,
- typeFSS,
- &keywd,
- &returnedType,
- (Ptr) &dtpFSS,
- sizeof(FSSpec),
- &actualSize) ;
- AEDisposeDesc(&dtpList) ;
- }
-
- // see how many descriptor items are in the list
- // this is the number of documents we want to open
- err = AECountItems(&docList,&itemsInList) ;
-
- // now get each descriptor record from the list
- // coerce the returned data to an FSSpec record, and
- // open the asoociated file
-
- for (index=1; index <= itemsInList && err == noErr; index++)
- {
- err = AEGetNthPtr( &docList,
- index,
- typeFSS,
- &keywd,
- &returnedType,
- (Ptr)&myFSS,
- sizeof(myFSS),
- &actualSize) ;
- if (err == noErr)
- {
- if (draggedToDTP)
- err = appHandlePDOC( &myFSS, &dtpFSS ) ;
- else
- err = appHandlePDOC( &myFSS, nil ) ;
- }
- }
- }
- err = AEDisposeDesc(&docList) ;
- }
- return err ;
- }
- #endif
-
-
- /*------------------------------------------------------------------------------*\
- HandleQUIT
- *------------------------------------------------------------------------------*
- This routine processes the 'QUIT' AppleEvent
- and passes it to the framework.
- \*------------------------------------------------------------------------------*/
- pascal OSErr HandleQUIT ( AppleEvent *theAppleEvent, AppleEvent *reply, long refCon )
- {
- OSErr err ;
-
- err = MyGotRequiredParams(theAppleEvent) ;
-
- if (err == noErr)
- err = appHandleQUIT() ;
-
- if (err == noErr)
- {
- AEDisposeDesc(&gSelfAddress) ; // Dispose of my self-addressed descriptor.
- AEDisposeDesc(&gSelfPSNAddress) ; // Dispose of my self-addressed descriptor.
- }
- return err ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- SendQUIT
- *------------------------------------------------------------------------------*
- This routine sends the 'QUIT' AppleEvent to this application to
- terminate this app. This routine is called by DoAppQuitCommand.
- \*------------------------------------------------------------------------------*/
- void SendQUIT ( void )
- {
- AppleEvent myAppleEvent, reply ;
-
- // Create the Apple Event.
- FailIfErr(AECreateAppleEvent( kCoreEventClass,
- kAEQuitApplication,
- &gSelfAddress,
- kAutoGenerateReturnID,
- kAnyTransactionID,
- &myAppleEvent)) ;
-
- // Send the Apple Event.
- FailIfErr(AESend( &myAppleEvent,
- &reply,
- kAENoReply+kAENeverInteract,
- kAENormalPriority,
- kAEDefaultTimeout,
- nil,
- nil)) ;
-
- AEDisposeDesc(&myAppleEvent) ; // Dispose of the Apple Event.
- }
-
-
-
- /*------------------------------------------------------------------------------*\
- SendODOC
- *------------------------------------------------------------------------------*
- This routine sends the 'ODOC' AppleEvent to this application to
- open a document. This routine is called by DoAppOpenCommand.
- \*------------------------------------------------------------------------------*/
- void SendODOC (FSSpec *myFSSpec)
- {
- AppleEvent myAppleEvent ;
- AppleEvent defReply ;
- AEDescList docList ;
- OSErr ignoreErr ;
-
- myAppleEvent.dataHandle = nil ;
- docList.dataHandle = nil ;
- defReply.dataHandle = nil ;
-
- // Create empty list and add one file spec
- FailIfErr(AECreateList(nil,0,false, &docList)) ;
-
- FailIfErr(AEPutPtr(&docList,1,typeFSS,(Ptr)myFSSpec,sizeof(FSSpec))) ;
-
- FailIfErr(AECreateAppleEvent( kCoreEventClass,
- kAEOpenDocuments,
- &gSelfAddress,
- kAutoGenerateReturnID,
- kAnyTransactionID,
- &myAppleEvent)) ;
-
- // Put Params into our event and send it
-
- FailIfErr(AEPutParamDesc( &myAppleEvent,
- keyDirectObject,
- &docList)) ;
-
- FailIfErr(AESend( &myAppleEvent,
- &defReply,
- kAENoReply+kAENeverInteract,
- kAENormalPriority,
- kAEDefaultTimeout,
- nil,
- nil)) ;
-
-
- if (myAppleEvent.dataHandle)
- ignoreErr = AEDisposeDesc(&myAppleEvent) ;
-
- if (docList.dataHandle)
- ignoreErr = AEDisposeDesc(&docList) ;
- }
-
-
-
- #if PIGS_SHELL_PRINT
- /*------------------------------------------------------------------------------*\
- SendPDOC
- *------------------------------------------------------------------------------*
- This routine sends the 'PDOC' AppleEvent to this application to
- print a document. This routine is called by DoAppPrintCommand and
- DoAppPrintOneCommand.
- \*------------------------------------------------------------------------------*/
- void SendPDOC (FSSpec *myFSSpec)
- {
- AppleEvent myAppleEvent ;
- AppleEvent defReply ;
- AEDescList docList ;
- OSErr ignoreErr ;
-
- myAppleEvent.dataHandle = nil ;
- docList.dataHandle = nil ;
- defReply.dataHandle = nil ;
-
- // Create empty list and add one file spec
- FailIfErr(AECreateList(nil,0,false, &docList)) ;
-
- FailIfErr(AEPutPtr(&docList,1,typeFSS,(Ptr)myFSSpec,sizeof(FSSpec))) ;
-
- FailIfErr(AECreateAppleEvent( kCoreEventClass,
- kAEPrintDocuments,
- &gSelfAddress,
- kAutoGenerateReturnID,
- kAnyTransactionID,
- &myAppleEvent)) ;
-
- // Put Params into our event and send it
-
- FailIfErr(AEPutParamDesc( &myAppleEvent,
- keyDirectObject,
- &docList)) ;
-
- FailIfErr(AESend( &myAppleEvent,
- &defReply,
- kAENoReply+kAENeverInteract,
- kAENormalPriority,
- kAEDefaultTimeout,
- nil,
- nil)) ;
-
-
- if (myAppleEvent.dataHandle)
- ignoreErr = AEDisposeDesc(&myAppleEvent) ;
-
- if (docList.dataHandle)
- ignoreErr = AEDisposeDesc(&docList) ;
- }
- #endif
-
-
- /**\
- |**| ==============================================================================
- |**| PRIVATE FUNCTIONS
- |**| ==============================================================================
- \**/
-
-
- /*------------------------------------------------------------------------------*\
- appHandleOAPP
- *------------------------------------------------------------------------------*
- This routine handles the 'OAPP' AppleEvent for the framework.
- This routine is called by HandleOAPP.
- \*------------------------------------------------------------------------------*/
- static OSErr appHandleOAPP ( void )
- {
- OSErr err = noErr ;
- winHandle win;
- AllocProcPtr allocProc = nil ;
- short i ;
-
- // determine what type of winHandles we need to auto-new
- for ( i=0; !err && i<gAllocProcMapCount; i++ )
- {
- if (gAllocProcMap[i].autoNew)
- {
- allocProc = gAllocProcMap[i].AllocProc ;
-
- // create a new winHandle of the propper type
- err = NewWinHandle( &win, allocProc ) ;
- WarnIfErr( err ) ;
- if (err) return err;
-
- err = CallWinNewProc( win ) ;
- if ( err != noErr )
- DisposeWinHandle( win ) ;
-
- if ( err == kWasAlreadyOpen )
- err = noErr ;
- }
- }
-
- return err ;
- }
-
-
- #if PIGS_SHELL_PRINT
- /*------------------------------------------------------------------------------*\
- appHandlePDOC
- *------------------------------------------------------------------------------*
- This routine handles the 'PDOC' AppleEvent for the framework.
- This routine is called by HandlePDOC.
- \*------------------------------------------------------------------------------*/
- static OSErr appHandlePDOC ( FSSpec *spec, FSSpec *dtp )
- {
- OSErr err = noErr ;
- winHandle win ;
- FInfo info ;
- AllocProcPtr allocProc = nil ;
- Boolean wasAlreadyOpen = false ;
- short i ;
-
- // get the file info for the FSSpec
- err = FSpGetFInfo(spec, &info) ;
- if ( err != noErr ) return err ;
-
- // determine what type of winHandle we need
- for ( i=0; i<gAllocProcMapCount; i++ )
- {
- if ( (gAllocProcMap[i].filetype == info.fdType)
- && (gAllocProcMap[i].printable == true ) )
- allocProc = gAllocProcMap[i].AllocProc ;
- }
- if ( allocProc==nil )
- return WarnIfErr( eWarnCantOpenFile ) ;
-
- // create a new winHandle of the proper type
- err = NewWinHandle( &win, allocProc ) ;
- WarnIfErr( err ) ;
- if (err) return err ;
-
- // set this field of the winHandle so that the doc can be opened
- SetWinFSSpec ( win, spec ) ;
-
- err = CallWinOpenProc ( win ) ;
- if ( err != noErr ) //if an error occured
- DisposeWinHandle( win ) ;
-
- if ( err == kWasAlreadyOpen )
- {
- wasAlreadyOpen = true ;
- err = noErr ;
- }
-
- if ( err ) return err ;
-
- // we know the document window is frontmost because we opened it
- win = GetWindowWinHandle( FrontWindow() ) ;
- if ( win == nil ) return nilHandleErr ;
-
- // if ( dtp && GXPrinting_present()==noErr )
- // GXSelectJobOutputPrinter( GetWinGXJob(win) , dtp->name) ;
- DoAppPrintLoop( win ) ; // print w/o dialogs
-
- if( !wasAlreadyOpen ) // do we need to close it?
- CallWinCloseProc( win ) ; // close the window
-
- return err ;
- }
- #endif
-
-
- /*------------------------------------------------------------------------------*\
- appHandleODOC
- *------------------------------------------------------------------------------*
- This routine handles the 'ODOC' AppleEvent for the framework.
- This routine is called by HandleODOC.
- \*------------------------------------------------------------------------------*/
- static OSErr appHandleODOC ( FSSpec *spec )
- {
- OSErr err = noErr ;
- winHandle win ;
- FInfo info ;
- AllocProcPtr allocProc = nil ;
- OSType subtype = nil;
- short i ;
-
- // get the file info for the FSSpec
- err = FSpGetFInfo(spec, &info) ;
- if ( err != noErr ) return err ;
-
- // determine what type of winHandle we need
- for ( i=0; i<gAllocProcMapCount; i++ )
- {
- if (gAllocProcMap[i].filetype == info.fdType)
- {
- allocProc = gAllocProcMap[i].AllocProc ;
- subtype = gAllocProcMap[i].subtype ;
- }
- }
- if ( allocProc==nil )
- return WarnIfErr( eWarnCantOpenFile ) ;
-
- // create a new winHandle of the proper type
- err = NewWinHandle( &win, allocProc ) ;
- WarnIfErr( err ) ;
- if (err) return err ;
-
- // set these field of the winHandle so that the doc can be opened
- SetWinFSSpec ( win, spec ) ;
- SetWinSubtype ( win, subtype ) ;
-
- err = CallWinOpenProc ( win ) ;
- if ( err != noErr ) //if an error occured
- DisposeWinHandle( win ) ;
-
- if ( err == kWasAlreadyOpen )
- err = noErr ;
-
- return err ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- appHandleQUIT
- *------------------------------------------------------------------------------*
- This routine handles the 'QUIT' AppleEvent for the framework.
- This routine is called by HandleQUIT.
- \*------------------------------------------------------------------------------*/
- static OSErr appHandleQUIT ( void )
- {
- winHandle win;
-
- // close all windows
- do {
- win = FrontWin();
- CallWinCloseProc(win);
- }
- while (win);
-
- // signal to Quit
- gQuitFlag = true ;
- return noErr ;
- }
-